package frontend;

import backend.GeneralDatabase;
import backend.EmptyOutput;
import service.DatabaseManager;
import service.Register.RegisterResult;
import service.Register.RegisterService;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;

public class Register extends JFrame {
    private JTextField userText;
    private JPasswordField passwordText;
    private JPasswordField confirmText;
    private final RegisterService registerService;

    public Register() {
        try {
            this.registerService = new RegisterService(DatabaseManager.getInstance());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        initialize();
    }

    private void initialize() {
        setTitle("健康科普信息系统 - 注册");
        setSize(600, 500);
        setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        setLocationRelativeTo(null);

        JPanel panel = new JPanel(new GridBagLayout());
        panel.setBorder(BorderFactory.createEmptyBorder(30, 30, 30, 30));
        GridBagConstraints gbc = new GridBagConstraints();
        gbc.insets = new Insets(10, 10, 10, 10);

        // 标题
        JLabel titleLabel = new JLabel("新用户注册");
        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 18));
        gbc.gridx = 0;
        gbc.gridy = 0;
        gbc.gridwidth = 2;
        gbc.anchor = GridBagConstraints.CENTER;
        panel.add(titleLabel, gbc);

        // 用户名输入
        gbc.gridx = 0;
        gbc.gridy = 1;
        gbc.gridwidth = 1;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(new JLabel("用户名:"), gbc);

        userText = new JTextField(15);
        gbc.gridx = 1;
        gbc.anchor = GridBagConstraints.WEST;
        panel.add(userText, gbc);

        // 密码输入
        gbc.gridx = 0;
        gbc.gridy = 2;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(new JLabel("密码:"), gbc);

        passwordText = new JPasswordField(15);
        gbc.gridx = 1;
        gbc.anchor = GridBagConstraints.WEST;
        panel.add(passwordText, gbc);

        // 密码提示
        JLabel passwordHint = new JLabel("密码需包含大小写字母、数字和特殊字符，至少8位");
        passwordHint.setFont(new Font("微软雅黑", Font.PLAIN, 10));
        passwordHint.setForeground(Color.GRAY);
        gbc.gridx = 1;
        gbc.gridy = 3;
        panel.add(passwordHint, gbc);

        // 确认密码
        gbc.gridx = 0;
        gbc.gridy = 4;
        gbc.anchor = GridBagConstraints.EAST;
        panel.add(new JLabel("确认密码:"), gbc);

        confirmText = new JPasswordField(15);
        gbc.gridx = 1;
        gbc.anchor = GridBagConstraints.WEST;
        panel.add(confirmText, gbc);

        // 按钮
        JButton registerButton = new JButton("注册");
        JButton backButton = new JButton("返回登录");

        gbc.gridx = 0;
        gbc.gridy = 5;
        gbc.anchor = GridBagConstraints.CENTER;
        panel.add(registerButton, gbc);

        gbc.gridx = 1;
        panel.add(backButton, gbc);

        // 事件绑定
        registerButton.addActionListener(this::handleRegister);
        backButton.addActionListener(e -> {
            dispose();
            try {
                new Login().setVisible(true);
            } catch (Exception ex) {
                throw new RuntimeException(ex);
            }
        });

        add(panel);
    }

    private void handleRegister(ActionEvent e) {
        String username = userText.getText().trim();
        String password = new String(passwordText.getPassword());
        String confirmPassword = new String(confirmText.getPassword());

        if (username.isEmpty() || password.isEmpty()) {
            JOptionPane.showMessageDialog(this, "用户名和密码不能为空！", "输入错误", JOptionPane.WARNING_MESSAGE);
            return;
        }

        if (!password.equals(confirmPassword)) {
            JOptionPane.showMessageDialog(this, "两次输入的密码不一致！", "输入错误", JOptionPane.WARNING_MESSAGE);
            return;
        }

        RegisterResult result = registerService.registerUser(username, password, confirmPassword);

        switch (result.getStatus()) {
            case SUCCESS:
                JOptionPane.showMessageDialog(this, result.getMessage(), "成功", JOptionPane.INFORMATION_MESSAGE);
                dispose();
                try {
                    new Login().setVisible(true);
                } catch (Exception ex) {
                    throw new RuntimeException(ex);
                }
                break;

            case USER_EXISTS:
                JOptionPane.showMessageDialog(this, result.getMessage(), "用户名冲突", JOptionPane.WARNING_MESSAGE);
                break;

            case INVALID_INPUT:
                JOptionPane.showMessageDialog(this, result.getMessage(), "输入错误", JOptionPane.WARNING_MESSAGE);
                break;

            case ERROR:
                JOptionPane.showMessageDialog(this, result.getMessage(), "系统错误", JOptionPane.ERROR_MESSAGE);
                break;
        }
    }
}